package Components
{
	import Model.Task;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.HorizontalList;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.ClassFactory;
	import mx.core.IChildList;

	public class TaskRack extends HorizontalList
	{
		[Inspectable]
		public var linkLayer:IChildList;
		public var showDetached:Boolean = false;
		
		private var actualProvider:ArrayCollection;
		
		public function TaskRack()
		{
			super();
			this.itemRenderer = new ClassFactory(TaskTab);
			setStyle("paddingTop", 0);
			BindingUtils.bindProperty(this, "rowHeight", this, "height");
			this.selectable = false;
		}
		
		public override function set dataProvider(value:Object):void
		{
			actualProvider = value as ArrayCollection;
			super.dataProvider = value;
		}
		
		public override function createItemRenderer(data:Object):IListItemRenderer
		{
			var ret:TaskTab = super.createItemRenderer(data) as TaskTab;
			ret.height = this.height;
			ret.rack = this;
				
			ret.addEventListener("ShowingPostIt", function(evt:Event):void
			{
				HideAllTasks();	//when a postIt shows, hide all the others
			});
			
			return ret;
		}
		
		public function HideAllTasks():void
		{
			for(var i:int = 0; i<listContent.numChildren; i++)
			{
				var child:DisplayObject = listContent.getChildAt(i);	
				if(child is TaskTab)
					TaskTab(child).HidePostIt();
			}
		}
		
		public function getTabForTask(task:Task):TaskTab
		{
			for(var i:int=0; i<listContent.numChildren; i++)
				if(listContent.getChildAt(i) is TaskTab)
					if(TaskTab(listContent.getChildAt(i)).task == task)
						return TaskTab(listContent.getChildAt(i));
			return null;
		}
		
		public function removeData(task:Task):void
		{
			actualProvider.removeItemAt(actualProvider.getItemIndex(task));
		}
	}
}